package com.movie2.controller;

import com.movie2.model.entity.Activity;
import com.movie2.model.entity.Registration;
import com.movie2.service.ActivityService;
import com.movie2.service.RegistrationService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.management.monitor.StringMonitor;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/api/registration")
@CrossOrigin
@Slf4j
public class RegistrationController {
    @Autowired
    RegistrationService registrationService;
    @Autowired
    ActivityService activityService;

    @PostMapping("")//添加报名
    public Map<String,Object> addRegistration(@RequestBody Registration registration){
        log.info("添加报名信息：RegistrationController -> addRegistration");
        Map<String,Object> map=new HashMap<>();
        int aid=registration.getAid();
        int uid=registration.getUid();
        // System.out.println(registrationService.isExist(aid,uid));
        if(registrationService.isExist(aid,uid)==null) {
            int count=registrationService.insert(registration);
            Activity activity = activityService.selectByPrimaryKey(aid);
            activity.setNumber(registrationService.selectRegistrationCountByAid(aid));
            activityService.updateByPrimaryKey(activity);
            map.put("success","true");
        }else {
            map.put("success","false");
        }
        return map;
    }
}
